package com.shanzmoo.pay.util;

import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @author dingmeng
 * @title: SignBeanUtil
 * @projectName jyf-mall-server
 * @description: 签名检验串
 * @date 2021/4/417:37
 */
public class SignBeanUtil {
    public SignBeanUtil() {
    }

    public static SignBeanUtil.Sign SignTool() {
        return new SignBeanUtil.Sign((SignBeanUtil.Sign)null);
    }

    public static String FilterNullSign(Object object, String keyname, String keyvalue, String Filter, String charset) {
        SignBeanUtil.Sign signContent = SignTool();
        String sign = null;

        try {
            Field[] fields = null;
            fields = getBeanFields(object.getClass(), fields);

            for(int i = 0; i < fields.length; ++i) {
                Field f = fields[i];
                String name = f.getName();
                f.setAccessible(true);
                Object valueObject = f.get(object);
                if (valueObject != null && (Filter == null || Filter.equals("") || !Filter.equals(name))) {
                    String value = valueObject.toString();
                    if (value != null) {
                        signContent.putParam(name, value);
                    }
                }
            }

            signContent.putLastParam(keyname, keyvalue);
            String parm = signContent.getSignStr();
            sign = MD5Util.MD5Encode(parm, charset);
        } catch (Exception var13) {
            var13.printStackTrace();
        }

        return sign;
    }

    public static Field[] getBeanFields(Class cls, Field[] fs) {
        fs = (Field[]) ArrayUtils.addAll(fs, cls.getDeclaredFields());
        if (cls.getSuperclass() != null) {
            Class clsSup = cls.getSuperclass();
            fs = getBeanFields(clsSup, fs);
        }

        return fs;
    }

    public static class Sign {
        private Map<String, String> map;
        private String lastStr;

        private Sign(Sign sign) {
            this.lastStr = "";
            this.map = new HashMap();
        }

        public SignBeanUtil.Sign putParam(String key, String value) {
            this.map.put(key, value);
            return this;
        }

        public SignBeanUtil.Sign putParamFilter(String key, String value) {
//            if (!ApiUtil.isEmpty(value)) {
                this.map.put(key, value);
//            }

            return this;
        }

        public SignBeanUtil.Sign putParamFilterNull(String key, String value) {
            if (value != null) {
                this.map.put(key, value);
            }

            return this;
        }

        public SignBeanUtil.Sign putLastParam(String key, String value) {
            this.lastStr = "&" + key + "=" + value;
            return this;
        }

        public String getParam(String key) {
            return (String)this.map.get(key);
        }

        public String getSignStr() {
            List<String> keys = new ArrayList(this.map.keySet());
            Collections.sort(keys);
            String prestr = "";

            for(int i = 0; i < keys.size(); ++i) {
                String key = (String)keys.get(i);
                String value = (String)this.map.get(key);
                if (i == keys.size() - 1) {
                    prestr = prestr + key + "=" + value;
                } else {
                    prestr = prestr + key + "=" + value + "&";
                }
            }

            return prestr + this.lastStr;
        }
    }
}
